package com.aya.child.simple;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * HttpServletResponse 两种方式注入的线程安全演示
 */
@Controller
public class ResponseController {



    @RequestMapping("methodInject")
    public void methodInject(HttpServletResponse myResponse) throws IOException {
        myResponse.getWriter().println("methodInject");
    }




    private HttpServletResponse localResponse;

    @ModelAttribute
    public void local(HttpServletResponse response)  {
        this.localResponse = response;
    }

    @RequestMapping("localA")
    public void localA() throws IOException {
        this.localResponse .getWriter().println("localA");
    }

    @RequestMapping("localB")
    public void localB() throws IOException, InterruptedException {
        Thread.sleep(5000);
        this.localResponse .getWriter().println("localB");
    }


    @Autowired
    private HttpServletResponse response;

    @RequestMapping("memberA")
    public void memberA() throws IOException {
        response.getWriter().println("memberA");
    }

    @RequestMapping("memberB")
    public void memberB() throws IOException, InterruptedException {
        Thread.sleep(5000);
        response.getWriter().println("memberB");
    }
}
